<?php

require_once("db.php");

connect_db('junyi', 'admin123', 'bookmark');

$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789823fjsfsj";
$code_length = 32;

function genRandomString() {
  $string = "";
  global $code_length;
  global $chars;
  for ($p = 0; $p < $code_length; $p++) {
    $string .= $chars[mt_rand(0, strlen($chars))];
  }
  return $string;
}

while (True) {
  $code = genRandomString();
  echo "trying $code \n";
  if (!invitation_code_exists($code)) {
    echo "writing db \n";
    add_invitation_code($code);
    echo "$code is ready\n";
    break;
  } else {
    echo "in used, will generate new\n";
  }
}



?>